package com.moody.hibernate.dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.moody.hibernate.domain.DebtCollateral;
import com.moody.hibernate.domain.DebtCollateralId;
import com.moody.hibernate.domain.ReportData;

/**
 * A data access object (DAO) providing persistence and search support for
 * DebtCollateral entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.moody.hibernate.domain.DebtCollateral
 * @author MyEclipse Persistence Tools
 */

public class DebtCollateralDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(DebtCollateralDAO.class);
	// property constants
	public static final String SHARE = "share";
	public static final String INPUT_USER_ID = "inputUserId";
	public static final String INPUT_ORG_ID = "inputOrgId";
	public static final String INPUT_DATE = "inputDate";
	public static final String UPDATE_USER_ID = "updateUserId";
	public static final String UPDATE_ORG_ID = "updateOrgId";
	public static final String UPDATE_DATE = "updateDate";

	protected void initDao() {
		// do nothing
	}

	public void save(DebtCollateral transientInstance) {
		log.debug("saving DebtCollateral instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(DebtCollateral persistentInstance) {
		log.debug("deleting DebtCollateral instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public DebtCollateral findById(com.moody.hibernate.domain.DebtCollateralId id) {
		log.debug("getting DebtCollateral instance with id: " + id);
		try {
			DebtCollateral instance = (DebtCollateral) getHibernateTemplate()
					.get("com.moody.hibernate.domain.DebtCollateral", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(DebtCollateral instance) {
		log.debug("finding DebtCollateral instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding DebtCollateral instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from DebtCollateral as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByShare(Object share) {
		return findByProperty(SHARE, share);
	}

	public List findByInputUserId(Object inputUserId) {
		return findByProperty(INPUT_USER_ID, inputUserId);
	}

	public List findByInputOrgId(Object inputOrgId) {
		return findByProperty(INPUT_ORG_ID, inputOrgId);
	}

	public List findByInputDate(Object inputDate) {
		return findByProperty(INPUT_DATE, inputDate);
	}

	public List findByUpdateUserId(Object updateUserId) {
		return findByProperty(UPDATE_USER_ID, updateUserId);
	}

	public List findByUpdateOrgId(Object updateOrgId) {
		return findByProperty(UPDATE_ORG_ID, updateOrgId);
	}

	public List findByUpdateDate(Object updateDate) {
		return findByProperty(UPDATE_DATE, updateDate);
	}

	public List findAll() {
		log.debug("finding all DebtCollateral instances");
		try {
			String queryString = "from DebtCollateral";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public DebtCollateral merge(DebtCollateral detachedInstance) {
		log.debug("merging DebtCollateral instance");
		try {
			DebtCollateral result = (DebtCollateral) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(DebtCollateral instance) {
		log.debug("attaching dirty DebtCollateral instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(DebtCollateral instance) {
		log.debug("attaching clean DebtCollateral instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static DebtCollateralDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (DebtCollateralDAO) ctx.getBean("DebtCollateralDAO");
	}

	@SuppressWarnings("unchecked")
	public List<DebtCollateral> findByCollateralId(String collateralId) {
		List<DebtCollateral> list = new ArrayList<DebtCollateral>();
		
		try{
			String queryString = "from DebtCollateral as dc where dc.id.collateralId = ?";
			list = getHibernateTemplate().find(queryString,collateralId);
		}catch (Exception e){
			log.error("query error", e);
		}
		return list;
	}

	public List<DebtCollateral> findByDebtNo(String debtNo) {
List<DebtCollateral> list = new ArrayList<DebtCollateral>();
		
		try{
			String queryString = "from DebtCollateral as dc where dc.id.debtNo = ?";
			list = getHibernateTemplate().find(queryString,debtNo);
		}catch (Exception e){
			log.error("query error", e);
		}
		return list;
	}

	public void update(DebtCollateral transientInstance) {
		log.debug("updating DebtCollateral instance");
		try {
			getHibernateTemplate().update(transientInstance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
		
	}
}